﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Dtd.Schema;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Engine.Sgml.Matching {
    /// <summary>
    /// Represents the information about capture.
    /// </summary>
    [DebuggerDisplay("{Entity.Element} ({StartIndex}-{EndIndex}) Success = {Success}")]
    [StructLayout(LayoutKind.Auto)]
    internal struct Capture {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private CaptureEntity entity;
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private bool success;

        internal Capture(IModelGroupsNode model, SElement element) {
            entity = new CaptureEntity(model, element);
            success = true;
            }

        internal Capture(SElement element) {
            entity = new CaptureEntity(element);
            this.success = false;
            }

        internal Capture(Match match, bool success) {
            entity = new CaptureEntity(match);
            this.success = success;
            }

        internal Capture(IModelGroupsNode model) {
            entity = new CaptureEntity(model);
            success = false;
            }

        public CaptureEntity Entity { get { return entity; } }

        /// <summary>
        /// Gets a value indicating whether the match is successful.
        /// </summary>
        public bool Success { get { return success; } }

        public int StartIndex {
            get {
                if (entity.IsElement)
                    return entity.Element.OrderIndex;
                else if (entity.IsModelMatch)
                    return entity.Match.StartIndex;
                else
                    return 0;
                }
            }

        public int EndIndex {
            get {
                if (entity.IsElement)
                    return entity.Element.OrderIndex;
                else if (entity.IsModelMatch)
                    return entity.Match.EndIndex;
                else
                    return 0;
                }
            }
        }
    }